package com.menggudf.convert;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeanUtils;

public class EntityConvertUtil
{
  private static final Log log = LogFactory.getLog(EntityConvertUtil.class);
  
  public static <T> T entity2Model(Object entity, Class<T> modelClazz)
  {
    if (entity == null) {
      return null;
    }
    T model = null;
    try
    {
      model = modelClazz.newInstance();
      BeanUtils.copyProperties(entity, model);
    }
    catch (Exception e)
    {
      log.error("entity2Model failed", e);
      throw new RuntimeException(e);
    }
    return model;
  }
  
  public static <T> List<T> entity2Model(List<?> entityList, Class<T> modelClazz)
  {
    if ((entityList == null) || (entityList.isEmpty())) {
      return new ArrayList();
    }
    List<T> modelList = new ArrayList();
    for (Object entity : entityList) {
      modelList.add(entity2Model(entity, modelClazz));
    }
    return modelList;
  }
  
  public static <T> Set<T> entity2Model(Set<?> entitySet, Class<T> modelClazz)
  {
    if ((entitySet == null) || (entitySet.isEmpty())) {
      return new HashSet();
    }
    Set<T> modelSet = new HashSet();
    for (Object entity : entitySet) {
      modelSet.add(entity2Model(entity, modelClazz));
    }
    return modelSet;
  }
}
